{include file="public/header" /}
</head>
<body>
	<div id="main-container">
		{include file="public/menu" /}
		<div class="main">
			<div class="notice">{$position.url}</div>
			<div class="main_content">
				<div class="title">
					<span>{$position.name}</span>
				</div>

				<div class="ui_prompt">
                    <p class="ui_prompt_tit">温馨提示</p>
                    <p>为了防止升级过程中出现数据丢失等问题，建议您在升级前先备份数据库！！！<a href="{:url('data/index')}" target="_blank">【点此备份数据库】</a></p>
                </div> 

				<div class="layui-form" id="view">
				    <table class="layui-table mt10" lay-even="" lay-skin="row">
				        <tbody>
				            <tr>
				                <td align="center" style="padding:50px 0" class="mcolor font18" id="loading">请稍等，正在检查最新版......</td>
				            </tr>
				        </tbody>
				    </table>
				</div>
				<script type="text/html" id="template">
				<div class="table">
				    <table class="layuitable" lay-even="" lay-skin="row">
				        <thead>
				            <tr>
				                <th width="150px">新版本</th>
				                <th style="text-align:left;">更新日志</th>
				                <th width="150px">操作</th>
				            </tr> 
				        </thead>
				        <tbody>
				            {{#  layui.each(d, function(index, item){ }}
				            <tr>
				                <td>{{ item.version }}</td>
				                <td style="text-align:left;padding:10px 0px;">{{ item.content }}</td>
				                <td>
				                    <a href="javascript:;" data-version="{{ item.version }}" class="btn doUpgrade">更新至此版本</a>
				                </td>
				            </tr>
				            {{#  }); }}
				        </tbody>
				    </table>
				</div>
				</script>
			</div>
		</div>
	</div>
<script>
layui.use(['jquery', 'layer', 'laytpl'], function(){
    var $ = layui.jquery, layer = layui.layer;
    getVersion();
    // 执行升级，
    $(document).on('click', '.doUpgrade', function(){
        var that = $(this);
        layer.msg('正在获取 '+that.attr('data-version')+' 升级包....',{time:500000});
        $.ajax({
            type: "POST",
            url: '{:url("download")}',
            data: 'version='+that.attr('data-version'),
            success: function(res) {
                if (res.code == 1) {
                    layer.msg('升级包获取成功，正在安装 '+that.attr('data-version')+' ...', {time:50000});
                    $.ajax({
                        type: "POST",
                        url: '{:url("install")}',
                        data: 'file='+res.msg+'&version='+that.attr('data-version'),
                        success: function(res) {
                            layer.msg(res.msg, {}, function() {
                                location.href= '{:url()}';
                            });
                        }
                    });
                } else {
                    layer.msg(res.msg, {}, function(){
                        location.href= '{:url()}';
                    });
                }
            }
        });
        return false;
    });
});

// 获取可升级版本
function getVersion() {
    var $ = layui.jquery,laytpl = layui.laytpl;
    $('#loading').html('请稍等，正在检查最新版......');

    $.ajax({
        type: "POST",
        url: '{:url("lists")}',
        success: function(res) {
            if (res.code == 1) {
                var getTpl = template.innerHTML;
                if (res.data == '') {
                    $('#loading').html('您当前的版本号已经是最新了哦！');
                    return false;
                }
                laytpl(getTpl).render(res.data, function(html) {
                    view.innerHTML = html;
                });
            } else {
                $('#loading').html('<span style="color:red;">'+res.msg+'</span> <a href="javascript:;" onclick="getVersion()" class="mcolor2">点此刷新重试</a>');
            }
        }
    });
}
</script>
</body>
</html>